package com.csdj.springboottest.config;

import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;

import java.time.Duration;

@EnableCaching
@Configuration
public class CacheConfig {

//    @Bean
//    public CacheManager cacheManage(){
//        return EhCacheCacheManager
//    }

    @Bean("redisCacheManager")
    public CacheManager redisCache(RedisConnectionFactory factory,Jackson2JsonRedisSerializer serializer){

        RedisCacheConfiguration configuration = RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofMinutes(2));
        RedisSerializationContext.SerializationPair<Object> pair = RedisSerializationContext.SerializationPair.fromSerializer(serializer);
        configuration.serializeValuesWith(pair);
        return RedisCacheManager.RedisCacheManagerBuilder.fromConnectionFactory(factory)
                .withCacheConfiguration("configCache",configuration)
                .build();
    }
}
